package com.egene.tw.activity.member.liferecord;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.activity.member.HealthRecycleAdapter;
import com.egene.tw.activity.member.MoreDetailActivity_;
import com.egene.tw.activity.member.PDFDetailActivity_;
import com.egene.tw.activity.member.TextActivity_;
import com.egene.tw.activity.wiki.WikInfoActivity;
import com.egene.tw.activity.wiki.WikInfoActivity_;
import com.egene.tw.api.EgeneApi;
import com.egene.tw.base.BaseRecycleViewFragment;
import com.egene.tw.base.RecycleBaseAdapter;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.HealthItem;
import com.egene.tw.model.HealthList;
import com.egene.tw.model.HealthListItem;
import com.egene.tw.model.Suggest;
import com.egene.tw.model.net.ResponseList;
import com.egene.tw.model.report.LifeRecord;
import com.egene.tw.network.ApiConstant;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

@EFragment
public class HealthListV2Fragment extends BaseRecycleViewFragment {

    public static final String TYPE = "TYPE";
    private String type;

    @Bean
    UserDao userDao;

    LayoutInflater mInflater;

    List<LifeRecord> lifeRecordList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getString(TYPE);
        }
        mInflater = LayoutInflater.from(getActivity());

    }

    @Override
    protected boolean isNeedListDivider() {
        return true;
    }

    @Override
    protected RecycleBaseAdapter getListAdapter() {
        return new HealthRecycleAdapter();
    }


    @Override
    protected void sendRequestData() {
        fetchList();
    }

    void fetchList() {
        EgeneApi.getHealthList(userDao.getUser().getUserID(), new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                HealthList result = JsonUtil.parse(responseBody, HealthList.class);
                if (result != null) {
                    if (result.isSuccess()) {
                        updateLiftRecord(result.getLiferecorduserlist());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }
        });
    }



    private void updateLiftRecord(List<LifeRecord> lifeRecords) {
        if(mRecycleView == null){
            return;
        }
        lifeRecordList = lifeRecords;
        if (lifeRecords == null || lifeRecords.size() == 0) {
            return;
        }
        List<HealthListItem> listItems = new ArrayList<>();
        for (final LifeRecord lifeRecord : lifeRecords) {
            HealthListItem title1 = new HealthListItem();
            title1.setTitle(lifeRecord.getLiftrecorddateformat());
            title1.setViewType(HealthRecycleAdapter.CONTENT_1);
            listItems.add(title1);
//
//            if(i >= 7){
//                break;
//            }
//            i++;
//            View view = mInflater.inflate(R.layout.item_left_record, ll_left_record, false);
//            TextView tvContent = (TextView) view.findViewById(R.id.tvContent);
//            tvContent.setText(lifeRecord.getLiftrecorddateformat());
//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    String path = ApiConstant.BASE_URL + "/admin/ques/app_liferecord_view.aspx?userid=" + lifeRecord.getUserid() + "&liftrecorddate=" + lifeRecord.getLiftrecorddate();
//                    PDFDetailActivity_.intent(getActivity()).title(lifeRecord.getLiftrecorddateformat()).path(path).start();
//                }
//            });
//            ll_left_record.addView(view);
        }

        if (mCurrentPage == START_PAGE_INDEX) {
            mAdapter.clear();
        }
        mAdapter.addData(listItems);
        mAdapter.notifyDataSetChanged();
    }



    @Override
    public void loadMore() {
//        super.loadMore();
    }

    @UiThread
    void updateList(List<HealthListItem> list) {
        if (mCurrentPage == START_PAGE_INDEX) {
            mAdapter.clear();
        }
        mAdapter.addData(list);
        mAdapter.notifyDataSetChanged();
        executeOnLoadFinish();
    }

    @Override
    protected void onItemClick(View view, int position) {
//        HealthListItem item = (HealthListItem) mAdapter.getItem(position);
        LifeRecord lifeRecord = lifeRecordList.get(position);
        String path = ApiConstant.BASE_URL + "/admin/ques/app_liferecord_view.aspx?userid=" + lifeRecord.getUserid() + "&liftrecorddate=" + lifeRecord.getLiftrecorddate();
        PDFDetailActivity_.intent(getActivity()).title(lifeRecord.getLiftrecorddateformat()).path(path).start();

//        if(item.getViewType() == HealthRecycleAdapter.TITLE){
//            return;
//        }
//        if(item.isSuggest()){
//            TextActivity_.intent(getActivity()).title("详情").content(item.getZhaiyao()).start();
//            return;
//        }
//
//        if (item.isNoImage()) {
//            MoreDetailActivity_.intent(getActivity()).rid(item.getRid()).orderId(item.getOrderId()).start();
//        } else {
//            Intent intent = new Intent(getActivity(), WikInfoActivity_.class);
//            intent.putExtra(WikInfoActivity.ID, "" + item.getId());
//            startActivity(intent);
//        }
    }

}
